﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Gao.Poker.Domain;

namespace Gao.Poker.Services
{
    /// <summary>
    /// 比赛报名Service
    /// </summary>
    public class CompetitionEntryService : BaseService
    {

        private static CompetitionEntryService instance;

        public static CompetitionEntryService Instance()
        {
            return instance ?? (instance = new CompetitionEntryService());
        }

        public List<CompetitionEntry> Search(int CompetitionID)
        {
            return DbContext.CompetitionEntries.Where(a => a.CompetitionID == CompetitionID).OrderBy(a => a.OutState).ToList();
        }

        public List<CompetitionEntry> GetGamePerson(int CompetitionID)
        {
            return DbContext.CompetitionEntries.Where(a => a.CompetitionID == CompetitionID).Where(a=>a.OutState == 0).ToList();
        }

        public CompetitionEntry GetMember(String memberCardNo, int competitionID)
        {
            return DbContext.CompetitionEntries.FirstOrDefault(a => a.MemberCardNo.Equals(memberCardNo) && a.CompetitionID == competitionID);
        }

        public CompetitionEntry Save(CompetitionEntry competitionEntry)
        {
            if (competitionEntry == null) return null;

            if (competitionEntry.ID == 0)
            {
                DbContext.CompetitionEntries.Add(competitionEntry);
            }
            else
            {
                DbContext.CompetitionEntries.Attach(competitionEntry);
                DbContext.Entry(competitionEntry).State = EntityState.Modified;
            }

            Trace.WriteLine(DbContext.ToString());
            DbContext.SaveChanges();
            return competitionEntry;
        }

        public void Update(Competition competition,List<Ranking> rankings)
        {
            if(competition == null) return;
            foreach (var ranking in rankings)
            {
                var ce = DbContext.CompetitionEntries.Where(a => a.CompetitionID == ranking.CompetitionID).FirstOrDefault(a => a.MemberCardNo == ranking.CardNo);
                var bouns = DbContext.Bounses.FirstOrDefault(a => a.Rank == ranking.Rank);
                var member = DbContext.Members.FirstOrDefault(a => ranking.CardNo.Equals(a.CardNo));
                if(ce != null && member != null)
                {
                    ce.Rank = ranking.Rank;
                    if(bouns == null) continue;
                    if (competition.DownsidePoints < competition.TotalPoints)
                    {
                        ce.Bonus = (decimal)(competition.TotalPoints * bouns.Scale / 100 * (1 - competition.ManCastRatio / (float)100));
                        member.Points += (int)(competition.TotalPoints * bouns.Scale / 100 * (1 - competition.ManCastRatio / (float)100));
                    }
                    else
                    {
                        ce.Bonus = (decimal)(competition.DownsideBonus * bouns.Scale / 100 * (1 - competition.ManCastRatio / (float)100));
                        member.Points += (int)(competition.DownsideBonus * bouns.Scale / 100 * (1 - competition.ManCastRatio / (float)100));
                    }
                }
            }
            DbContext.SaveChanges();
        }
    }
}
